package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Activity;
import cn.wolfcode.domain.ActivityItem;
import cn.wolfcode.domain.Choice;
import cn.wolfcode.service.IActivityItemService;
import cn.wolfcode.service.IActivityService;
import cn.wolfcode.service.IChoiceService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.List;

@Controller
@RequestMapping("/choice")
public class ChoiceController {

    @Autowired
    private IChoiceService choiceService;
    @Autowired
    private IActivityItemService activityItemService;
    @Autowired
    private IActivityService activityService;

    @RequiredPermission(name="投票结果页面",expression="choice:list")
    @RequestMapping("/list")
    public String list(Model model,Long id){
        List<ActivityItem> activityItems = activityItemService.getAct(id);
        model.addAttribute("activityItems",activityItems);
        Activity activity = activityService.get(id);
        model.addAttribute("activity",activity);
        Integer totalNum = activityItemService.getNum(id);
        model.addAttribute("totalNum",totalNum);
        return "activity/result";
    }


    @RequiredPermission(name="投票结果删除",expression="choice:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           choiceService.delete(id);
        }
        return "redirect:/choice/list";
    }


    @RequiredPermission(name="投票结果新增/编辑",expression="choice:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Choice choice) {
        if (choice.getId() == null) {
            choiceService.save(choice);
        } else {
            choiceService.update(choice);
        }
        return "redirect:/choice/list";
    }


    @RequiredPermission(name="投票结果新增/编辑",expression="choice:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("choice",choiceService.get(id));
        }
        return "choice/input";
    }
}
